#include "nrf_spi.h"

/**
 * @brief  nrfspi初始化
 * @param  void
 * @retval void
 */
void nrfspi_init(void)
{		
		GPIO_InitTypeDef GPIO_Initure;

    GPIO_Initure.Pin   = NRF_CE_GPIO_PIN;
    GPIO_Initure.Mode  = GPIO_MODE_OUTPUT_PP;  
    GPIO_Initure.Pull  = GPIO_NOPULL;  
    GPIO_Initure.Speed = GPIO_SPEED_HIGH;    	
    HAL_GPIO_Init(NRF_CE_GPIO, &GPIO_Initure);
	
	
    GPIO_Initure.Pin   = NRF_CSN_GPIO_PIN;
    GPIO_Initure.Mode  = GPIO_MODE_OUTPUT_PP;  
    GPIO_Initure.Pull  = GPIO_NOPULL;  
    GPIO_Initure.Speed = GPIO_SPEED_HIGH;    	
    HAL_GPIO_Init(NRF_CSN_GPIO, &GPIO_Initure);
		
		GPIO_Initure.Pin   = NRF_IQR_GPIO_PIN;
    GPIO_Initure.Mode  = GPIO_MODE_INPUT;  
    GPIO_Initure.Pull  = GPIO_PULLUP;      
    GPIO_Initure.Speed = GPIO_SPEED_HIGH;    	
    HAL_GPIO_Init(NRF_IQR_GPIO, &GPIO_Initure);
		
		NRF_CE(0);
		NRF_CSN(0);
}

//void spi_SetClk(SPI_HandleTypeDef *hspi, uint8_t CPOL, uint8_t CPHA)
//{
//  hspi1.Instance = SPI1;
//  hspi1.Init.Mode = SPI_MODE_MASTER;
//  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
//  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
//  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
//  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
//  hspi1.Init.NSS = SPI_NSS_SOFT;
//  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
//  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
//  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
//  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
//  hspi1.Init.CRCPolynomial = 10;
//	
//	if (CPOL != 0) {
//		hspi->Init.CLKPolarity = SPI_POLARITY_HIGH;
//	}
//	
//	if (CPOL != 0) {
//		hspi->Init.CLKPhase = SPI_PHASE_2EDGE;
//	}
//	
//  if (HAL_SPI_Init(&hspi1) != HAL_OK)
//  {
//    Error_Handler();
//  }
//}
/**
 * @brief  nrfspi读写一个字节
 * @param  void
 * @retval void
 */
static uint8_t nrfspi_WriteRead(uint8_t data)
{
	uint8_t nrfspi_Read_datd=0;

//	spi_SetClk(&NRF_SPI, 0,0);
	
	HAL_SPI_TransmitReceive(&NRF_SPI, &data, &nrfspi_Read_datd, 1, 0xff);
	
	return nrfspi_Read_datd;
}

/**
 * @brief  nrfspi只读一个字节
 * @param  void
 * @retval void
 */
static uint8_t nrfspi_Read(void)
{
    uint8_t nrfspi_Read_datd=0;
    
//	spi_SetClk(&NRF_SPI, 0,0);
	
    HAL_SPI_Receive(&NRF_SPI, &nrfspi_Read_datd, 1, 0xff);
	
		return nrfspi_Read_datd;
}


/**
 * @brief  nrfspi写入一个字节
 * @param  void
 * @retval void
 */
uint8_t nrfspi_WriteByte(uint8_t addr, uint8_t order, uint8_t mode)
{
		uint8_t retu_da=0;  
    
    NRF_CSN(0);
    
		if(mode) {
        addr = addr | 0x20; //设置为写
    }		
    
    retu_da = nrfspi_WriteRead(addr);

    nrfspi_WriteRead(order);
    
    NRF_CSN(1);
    return retu_da;
}

/**
 * @brief  nrfspi接收一个字节
 * @param  void
 * @retval void
 */
uint8_t nrfspi_ReadByte(uint8_t addr)
{
		NRF_CSN(0);
		uint8_t spi_data = 0;
    nrfspi_WriteRead(addr);
    spi_data = nrfspi_Read();
    NRF_CSN(1);
    return spi_data;
}


/**
 * @brief  nrfspi写一个数组
 * @param  void
 * @retval void
 */
void nrfspi_WriteBuff(uint8_t addr, uint8_t *databuff, uint8_t len, uint8_t mode)
{
	NRF_CSN(0);
 	uint8_t lens;
    
	if (mode) {
			addr = addr | 0x20;     //设置为写
  }		

  nrfspi_WriteRead(addr);
    
	for (lens = 0; lens < len; lens++) {
      nrfspi_WriteRead(databuff[lens]);
	}
    NRF_CSN(1);
}

/**
 * @brief  nrfspi读取一个数组
 * @param  void
 * @retval void
 */
void nrfspi_ReadBuff(uint8_t addr, uint8_t *databuff, uint8_t len)
{
	NRF_CSN(0);
	int8_t lens;

  nrfspi_WriteRead(addr);
    
	for (lens  = 0; lens < len; lens++) {
		databuff[lens] = nrfspi_Read();
	}
    
	NRF_CSN(1);
}
